﻿<!DOCTYPE html>
<html>
<head>
    <meta charset="UTF-8">
    <title>Json内容查看器</title>
    <link href="../third/jsoneditor/jsoneditor.css" rel="stylesheet" type="text/css">
    <script src="../third/jsoneditor/jsoneditor.min.js"></script>
    <style>
        #jsoneditor{
            min-height: 200px;
            height: 400px;
        }
    </style>

</head>
<body>
    <div id="jsoneditor" style="width: 100%;"></div>

    <div style="position: relative;float: right;display: none" id="myview">
        <button id="templatebtn" style="height: 28px;width: 70px;">样板</button>
    </div>

    <script src="../lib/require.js"></script>
<script>
    require(["./ladder-core"],function() {
        var templateText=Ladder.getRequestParam("tpl");
        $("#jsoneditor").height($(window).height()-20);
        var container=Ladder.dom("jsoneditor");
        var options = {
            search:true,
            mode: 'tree',
            modes: ['code', 'form', 'text', 'tree', 'view'], // allowed modes
            onError: function (err) {
                alert(err.toString());
            },
            onModeChange: function (newMode, oldMode) {
                if(newMode=="text"){
                    $(".jsoneditor-menu").append($("#myview").fadeIn());
                }else{
                    $(document.body).append($("#myview").hide());
                }
            }
        };
        window.editor=new JSONEditor(container,options);
        var jsonData=Ladder.getStore("tempData",false);

        if(jsonData&&/^(\{|\[)[\w\W]*(}|])/.test(jsonData)){
            var obj = Ladder.toJSON(jsonData);
//            for(var p in obj){
//                if(typeof (obj[p]) === "string"){
//                    //obj[p]=(obj[p]+"").replace(/(\n)/g,"<br>");
//                }
//            }
            editor.set(obj);
            editor.expandAll();
        }else{
            editor.setMode("text");

            editor.setText(jsonData.replace(/(\\n)/g,"\n"));
        }
        setTimeout(function () {
            if(editor.getMode()=="text")
                $(".jsoneditor-menu").append($("#myview").fadeIn());
        });
        $("#templatebtn").click(function () {
            if(templateText){
                debugger;
                var content = decodeURIComponent(templateText);
                if(/^%/.test(content))content=decodeURIComponent(content);
                editor.set(Ladder.toJSON(content));
            }
        });

    });
</script>




</body>
</html>